package core.common;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;


public class Configurations {

	private final static String CONFIG_FILE = "config.properties";
	
	private final static String CONNECTION = "connectionURL";
	private final static String DB_USER = "DBUser";
	private final static String DB_PASS = "DBPassword";
	private final static String DB_SCHEMA = "DBSchema";
	private final static String CONNECTION_POOL_COUNT = "ConnectionPoolCount";
	private final static String THREAD_POOL_COUNT = "ThreadPoolCount";
	private final static String BOOK_SEARCH_LIMIT = "BookSearchLimit";
	private final static String NEWS_FEED_LIMIT = "NewsFeedLimit";
	
	private final static String DEFAULT_CONNECTION = "localhost:3305";
	private final static String DEFAULT_DB_USER = "DbMysql03";
	private final static String DEFAULT_DB_PASS = "DbMysql03";
	private final static String DEFAULT_DB_SCHEMA = "DbMysql03";
	private final static int DEFAULT_CONN_POOL_COUNT = 10;
	private final static int DEFAULT_THREAD_POOL_COUNT = 50;
	private final static int DEFAULT_BOOK_SEARCH_LIMIT = 50;
	private final static int DEFAULT_NEWS_FEED_LIMIT = 50;

	private static String connectionUrl = DEFAULT_CONNECTION;
	private static String dbUser = DEFAULT_DB_USER;
	private static String dbPass = DEFAULT_DB_PASS;
	private static String dbSchema = DEFAULT_DB_SCHEMA;
	private static int connPoolCount = DEFAULT_CONN_POOL_COUNT;
	private static int threadPoolCount = DEFAULT_THREAD_POOL_COUNT;
	private static int bookSearchLimit = DEFAULT_BOOK_SEARCH_LIMIT;
	private static int newsFeedLimit = DEFAULT_NEWS_FEED_LIMIT;
	
	private Configurations() { }
	
	static {
		
		Properties properties = new Properties();
		try {
			
			properties.load(new FileReader(CONFIG_FILE));
			
			connectionUrl = properties.getProperty(CONNECTION, DEFAULT_CONNECTION);
			dbUser = properties.getProperty(DB_USER, DEFAULT_DB_USER);
			dbPass = properties.getProperty(DB_PASS, DEFAULT_DB_PASS);
			dbSchema = properties.getProperty(DB_SCHEMA, DEFAULT_DB_SCHEMA);
			
			connPoolCount = Integer.parseInt(
					properties.getProperty(CONNECTION_POOL_COUNT, DEFAULT_CONN_POOL_COUNT + ""));

			threadPoolCount = Integer.parseInt(
					properties.getProperty(THREAD_POOL_COUNT, DEFAULT_THREAD_POOL_COUNT + ""));

			bookSearchLimit = Integer.parseInt(
					properties.getProperty(BOOK_SEARCH_LIMIT, DEFAULT_BOOK_SEARCH_LIMIT + ""));
			
			newsFeedLimit = Integer.parseInt(
					properties.getProperty(NEWS_FEED_LIMIT, DEFAULT_NEWS_FEED_LIMIT + ""));
			
		} catch (FileNotFoundException e) {
		} catch (IOException | NumberFormatException e) {
		}
		
	}

	public static String getConnectionUrl() {
		return connectionUrl;
	}

	public static String getDbUser() {
		return dbUser;
	}

	public static String getDbPass() {
		return dbPass;
	}

	public static String getDbSchema() {
		return dbSchema;
	}

	public static int getConnectionPoolCount() {
		return connPoolCount;
	}

	public static int getThreadPoolCount() {
		return threadPoolCount;
	}
	
	public static int getBookSearchLimit() {
		return bookSearchLimit;
	}
	
	public static int getNewsFeedLimit() {
		return newsFeedLimit;
	}
}
